<template>
  <div class="level-list">
    <el-tabs v-model="activeName">
      <el-tab-pane label="基础信息" name="first">
        <levelForm :disabled="true" :levelType="$route.params.type" :formInfo="formInfo"></levelForm>
      </el-tab-pane>
      <el-tab-pane label="质控点信息" name="second">
        <!-- 搜索 -->
        <listSearch @search="search"></listSearch>
        <!-- 表格 -->
        <myTable :column="column" :tableData="tableData" :total="total" @changePage="changePage">
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="handle('detail', scope.row)">详情</el-button>
              <el-button v-if="(adminId == userInfo.id && checkShow) || (dutyUser == userInfo.id && checkReleaseShow)" type="text" @click="handle('standard', scope.row)">设置标准值</el-button>
            </template>
          </el-table-column>
        </myTable>
      </el-tab-pane>
    </el-tabs>
    <div class="back-btn">
      <!--目标标准管理员只有“审核”操作-->
      <el-button v-if="adminId == userInfo.id && checkShow" type="primary" @click="handle('check', tableData)">审核{{userInfo.adminId}}</el-button>
      <el-button v-if="dutyUser == userInfo.id && checkReleaseShow" type="primary" @click="handle('checkRelease', tableData)">发布</el-button>
      <back-button></back-button>
    </div>

    <!-- 审核 -->
    <checkDialog v-if="dialogParams.visible" :dialog-params="dialogParams" @check="check"></checkDialog>
    <!-- 质控点详情 -->
    <!-- 为了诊改对象有多类时，展示每个质控点的标准值--引用标准管理的设置标准值界面-->
    <setStandardValue v-if="addParams.visible" :dialog-params="addParams" :point-form="pointForm" @getList="getList"></setStandardValue>
  </div>
</template>

<script>
import listSearch from '@/components/search/listSearch'
import myTable from '@/components/table/index.vue'
import levelForm from '@/views/targetSystem/targetManage/module/levelFrom'
import checkDialog from './module/check'
// import addPoint from '@/views/targetSystem/targetManage/module/addPoint'
import setStandardValue from '@/views/targetSystem/standardManage/module/setStandardValue'
import { getPointList, getFormWorkById, checkTarget, releaseTarget } from '../../../api/target'
import { updateWaitDealState } from '../../../api'
import { getStorage, filterParams } from '../../../config/utils'
import { Message } from 'element-ui'

export default {
  name: 'targetCheckList',
  components: {
    listSearch,
    myTable,
    levelForm,
    checkDialog,
    // addPoint
    setStandardValue
  },
  data() {
    return {
      activeName: 'first',
      searchFrom: {
        targetStandardId: '',
        indexOne: '',
        indexTwo: '',
        indexThree: '',
        name: '',
        pageSize: 10,
        pageIndex: 1
      },
      column: [
        {
          prop: 'indexOne',
          label: '一级指标'
        },
        {
          prop: 'indexTwo',
          label: '二级指标'
        },
        {
          prop: 'indexThree',
          label: '三级指标'
        },
        {
          prop: 'name',
          label: '质控点'
        },
        {
          prop: 'intension',
          label: '质控点内涵'
        },
        {
          prop: 'diagnosisStandard',
          label: '诊断标准'
        },
        {
          prop: 'belongPartName',
          label: '责任部门'
        },
        {
          prop: 'valueType',
          label: '值类型'
        },
        {
          prop: 'expectedValue',
          label: '预期值'
        },
        {
          prop: 'standardValue',
          label: '标准值'
        },
        {
          prop: 'standardState',
          label: '确认状态',
          standardState: true
        },
        {
          prop: 'targetValue',
          label: '目标值'
        },
        {
          prop: 'isWarning',
          label: '是否预警',
          asyncData: this.$config.yesOrNo
        },
        {
          prop: 'isOptional',
          label: '是否可选',
          asyncData: this.$config.isOptional
        }
      ],
      tableData: [],
      total: 0,
      formInfo: null,
      // 审核相关参数
      dialogParams: {
        visible: false,
        title: '审核',
        type: 'check',
        id: '',
        okText: '通过'
      },
      addParams: {
        visible: false,
        title: '查看质控点',
        editFlag: false,
        type: ''
      },
      pointForm: {},
      dutyUser: '', // 目标标准负责人
      adminId: '', // 目标标管理员
      userInfo: getStorage('userInfo'),
      checkShow: true,
      checkReleaseShow: true
    }
  },
  created() {
    this.searchFrom.targetStandardId = this.$route.query.id
    this.getList()
    this.getMyTargetById()
  },
  methods: {
    // 搜索
    search(searchFrom) {
      this.searchFrom.pageIndex = 1
      this.searchFrom.indexOne = searchFrom.indexOne
      this.searchFrom.indexTwo = searchFrom.indexTwo
      this.searchFrom.indexThree = searchFrom.indexThree
      this.searchFrom.name = searchFrom.name
      this.searchFrom.belongPart = searchFrom.belongPart
      this.getList()
    },
    // 查询质控点列表
    getList() {
      const params = filterParams(this.searchFrom)
      getPointList(params)
        .then(res => {
          this.tableData = res.dataList || []
          this.total = res.total || 0
        })
        .catch(() => {})
    },
    // 查询我的目标标准详情
    getMyTargetById() {
      if (this.searchFrom.targetStandardId) {
        getFormWorkById(this.searchFrom.targetStandardId)
          .then(res => {
            this.formInfo = res || {}
          })
          .catch(() => {})
      }
    },
    // 审核或发布
    check(examineOpinion, state) {
      const params = {
        ...this.formInfo,
        state: +state,
        examineIdea: examineOpinion
      }
      if (this.dialogParams.type == 'check') {
        if (params.state == 3) {
          params.state = 4
        }
        checkTarget(params)
          .then(res => {
            this.checkShow = false
            this.$message.success('操作成功')
            this.dialogParams.visible = false
            if (this.$route.query.isWaitDeal) {
              updateWaitDealState(this.$route.query.waitDealId, 1)
            }
          })
          .catch(() => {})
      } else if (this.dialogParams.type == 'checkRelease') {
        if (params.state == 2) {
          params.state = 3
        } else {
          params.state = 4
        }
        releaseTarget(params)
          .then(res => {
            this.checkReleaseShow = false
            this.checkShow = false
            this.$message.success('操作成功')
            this.dialogParams.visible = false
            if (this.$route.query.isWaitDeal) {
              updateWaitDealState(this.$route.query.waitDealId, 1)
            }
          })
          .catch(() => {})
      }
    },
    // 切换分页
    changePage(searchForm) {
      this.searchFrom.pageIndex = searchForm.pageIndex
      this.searchFrom.pageSize = searchForm.pageSize
      this.getList()
    },
    handle(type, data) {
      switch (type) {
        case 'detail':
          this.addParams = {
            // visible: true,
            // title: '查看质控点',
            // type: type,
            // id: data,
            // data: data,
            // pointType: 'show'
            visible: true,
            title: '查看质控点',
            pointType: type,
            type: type,
            data: data,
            id: data.id,
            editFlag: false,
            isCustomPoint: !(data.sourceId && data.sourceId > 0),
            rectificationRangeId: this.$route.query.rectificationRangeId
          }
          break
        case 'check':
          // 先检测每个质控点是否填写了标准值:
          for (const item of data) {
            if (!(item.standardValue || item.standardValue == 0) && item.isOptional === 1) {
              Message.error(`质控点"${item.name}"未设置标准值，不允许审核！`)
              return
            }
          }
          this.dialogParams = {
            visible: true,
            title: '管理员审核',
            okText: '通 过',
            type: type
          }
          break
        case 'checkRelease':
          // 先检测每个质控点是否填写了标准值:
          for (const item of data) {
            if (!(item.standardValue || item.standardValue == 0) && item.isOptional === 1) {
              Message.error(`质控点"${item.name}"未设置标准值，不允许发布！`)
              return
            }
          }
          this.dialogParams = {
            visible: true,
            title: '负责人审核并发布',
            okText: '通过并发布',
            type: type
          }
          break
        case 'standard':
          // sourceId不为空表示下发的质控点，为空或不存在就是自定义的质控点
          this.addParams = {
            visible: true,
            title: '设置标准值',
            pointType: type,
            type: type,
            data: data,
            id: data.id,
            editFlag: true,
            isCustomPoint: !(data.sourceId && data.sourceId > 0),
            rectificationRangeId: this.$route.query.rectificationRangeId
          }
          break
      }
    },
    // 获取质控点详情
    getDetail() {
      getMyPointDetail(this.addParams.id)
        .then(res => {
          if (res) {
            this.pointForm = { ...res }
          }
        })
        .catch(() => {})
    }
  },
  watch: {
    $route: {
      handler(val) {
        // console.log(val.query, this.userInfo);
        // 如果index为2,默认打开second的tab页，默认显示质控点信息
        if (val.query.index == 2) {
          this.activeName = 'second'
        }
        this.dutyUser = val.query.dutyUser
        this.adminId = val.query.adminId
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style scoped>
</style>
